# -*- coding: utf-8 -*-
# @Author: Lemongman

import uuid
import tkinter
import pyautogui

from utils.single import singleton
"""
截图工具
"""


@singleton
class ScreenShot:
    def __init__(self):
        self.x_start, self.y_start = 0, 0
        self.x_end, self.y_end = 0, 0

        # 组件距离屏幕的位置
        self.x, self.y = 0, 0

        # 初始化tk
        self.tk = tkinter.Tk()
        # 初始化canvas，圈选区域
        self.cv = tkinter.Canvas(self.tk)
        self.rec = ''
        # 工具条
        self.tool = tkinter.Canvas(self.tk)

        # 绑定事件
        self.tk.bind('<Escape>', self.quit_tk)      # 退出截图
        self.tk.bind('<Button-1>', self.show_cv)    # 左键点击，显示截图区域
        self.tk.bind('<B1-Motion>', self.change_size)   # 改变截图区域大小
        self.tk.bind('<ButtonRelease-1>', self.btn_release)     # 释放鼠标左键，记录位置

        # 用于判断是否是截图操作
        self.is_shot = True

    def quit_tk(self, e):
        self.tk.destroy()

    def set_main_win(self):
        # 设置透明主窗口
        self.tk.overrideredirect(True)  # 隐藏标题栏
        self.tk.attributes('-alpha', 0.5)   # 透明度50%
        self.tk.geometry('{0}x{1}'.format(self.tk.winfo_screenwidth(), self.tk.winfo_screenheight()))
        self.tk.configure(bg='black')

    def show_cv(self, e):
        if self.is_shot:
            # 重置工具条
            if self.rec != '':
                self.tool.destroy()
                self.tool = tkinter.Canvas(self.tk)
                self.cv.destroy()
                self.cv = tkinter.Canvas(self.tk)

            self.cv.bind('<Enter>', self.cv_over)
            self.cv.bind('<Leave>', self.cv_leave)
            # 显示canvas
            self.x_start, self.y_start = e.x + self.x, e.y + self.y
            self.cv.configure(width=1)
            self.cv.configure(height=1)
            self.cv.config(highlightthickness=0)
            self.cv.place(x=e.x + self.x, y=e.y + self.y)
            self.rec = self.cv.create_rectangle(0, 0, 0, 0, outline='red', width=5)

    def cv_over(self, e):
        self.x, self.y = self.cv.winfo_x(), self.cv.winfo_y()

    def cv_leave(self, e):
        self.x, self.y = 0, 0

    def change_size(self, e):
        # 改变截图区域大小
        x = e.x - self.x_start
        y = e.y - self.y_start
        self.cv.configure(width=x)
        self.cv.configure(height=y)
        self.cv.coords(self.rec, 0, 0, x, y)

    def btn_release(self, e):
        # 释放鼠标左键
        self.x_end = e.x + self.x
        self.y_end = e.y + self.y
        if self.x_start != self.x_end and self.y_start != self.y_end and self.is_shot:
            self.set_tool(e)

    def set_tool(self, e):

        # 设置工具条
        self.tool.configure(width=50)
        self.tool.configure(height=50)
        self.tool.configure(bg='yellow')
        self.tool.configure(highlightthickness=0)   # 高亮厚度
        self.tool.place(x=e.x - 50, y=e.y)

        # 保存截图按钮
        save = self.tool.create_rectangle(5, 5, 45, 45, outline='red', width=5)
        self.tool.bind('<Button-1>', self.bind_adaptor(self.tool_click, save))
        self.tool.bind('<ButtonRelease-1>', self.bind_adaptor(self.tool_unclick, save))
        self.tool.bind('<Enter>', self.tool_over)
        self.tool.bind('<Leave>', self.tool_leave)

    def tool_click(self, e, save):
        if not self.is_shot:
            self.tool.itemconfig(save, fill='red')

    def tool_unclick(self, e, save):
        if not self.is_shot:
            self.tool.itemconfig(save, fill='yellow')
            self.cv.place_forget()
            self.tk.attributes('-alpha', 0)
            img = pyautogui.screenshot(region=[self.x_start, self.y_start,
                                               self.x_end - self.x_start, self.y_end - self.y_start])
            img.save(r'tmpImg/{}.png'.format(uuid.uuid1()))
            self.quit_tk(e)

    def tool_over(self, e):
        self.is_shot = False

    def tool_leave(self, e):
        self.is_shot = True

    @staticmethod
    def bind_adaptor(fun, *args):
        # 绑定函数适配，用于传参
        return lambda event, fun=fun, args=args: fun(event, *args)

    def start_ss(self):
        # 开始截图
        self.set_main_win()
        self.tk.mainloop()


if __name__ == '__main__':
    ss = ScreenShot()
    ss.start_ss()